home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  13.9 KB  |  522 lines

  1. /* Indentation functions.
  2.    Copyright (C) 1995 Board of Trustees, University of Illinois
  3.    Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994 
  4.    Free Software Foundation, Inc.
  5.  
  6. This file is part of XEmacs.
  7.  
  8. XEmacs is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with XEmacs; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* This file has been Mule-ized. */
  23.  
  24. /* Synched up with: Divergent from FSF. */
  25.  
  26.  
  27. #include <config.h>
  28. #include "lisp.h"
  29.  
  30. #include "buffer.h"
  31. #include "device.h"
  32. #include "extents.h"
  33. #include "faces.h"
  34. #include "frame.h"
  35. #include "glyphs.h"
  36. #include "insdel.h"
  37. #include "window.h"
  38.  
  39. /* Indentation can insert tabs if this is non-zero;
  40.    otherwise always uses spaces */
  41. int indent_tabs_mode;
  42.  
  43. /* Avoid recalculation by remembering things in these variables. */
  44.  
  45. /* Last value returned by current_column.
  46.  
  47.    Some things set last_known_column_point to -1
  48.    to mark the memoized value as invalid */
  49. static int last_known_column;
  50.  
  51. /* Last buffer searched by current_column */
  52. static struct buffer *last_known_column_buffer;
  53.  
  54. /* Value of point when current_column was called */
  55. static Bufpos last_known_column_point;
  56.  
  57. /* Value of MODIFF when current_column was called */
  58. static int last_known_column_modified;
  59.  
  60. static Bufpos
  61. last_visible_position (Bufpos pos, struct buffer *buf)
  62. {
  63.   Lisp_Object buffer;
  64.   Lisp_Object value;
  65.  
  66.   XSETBUFFER (buffer, buf);
  67.   value = Fprevious_single_property_change (make_number (pos), Qinvisible,
  68.                         buffer, Qnil);
  69.   if (NILP (value))
  70.     return 0; /* no visible position found */
  71.   else
  72.     /* #### bug bug bug!!! This will return the position of the beginning
  73.        of an invisible extent; this extent is very likely to be start-closed,
  74.        and thus the spaces inserted in `indent-to' will go inside the
  75.        invisible extent.
  76.  
  77.        Not sure what the correct solution is here.  Rethink indent-to? */
  78.     return XINT (value);
  79. }
  80.  
  81.  
  82.  
  83. /* Cancel any recorded value of the horizontal position.  */
  84.  
  85. void
  86. invalidate_current_column (void)
  87. {
  88.   last_known_column_point = -1;
  89. }
  90.  
  91. int
  92. column_at_point (struct buffer *buf, Bufpos init_pos, int cur_col)
  93. {
  94.   int col;
  95.   int tab_seen;
  96.   int tab_width = XINT (buf->tab_width);
  97.   int post_tab;
  98.   Bufpos pos = init_pos;
  99.  
  100.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  101.   col = tab_seen = post_tab = 0;
  102.  
  103.   while (1)
  104.     {
  105.       if (pos <= BUF_BEGV (buf))
  106.     break;
  107.  
  108.       pos--;
  109.       if (BUF_FETCH_CHAR (buf, pos) == '\t')
  110.     {
  111.       if (tab_seen)
  112.         col = ((col + tab_width) / tab_width) * tab_width;
  113.  
  114.       post_tab += col;
  115.       col = 0;
  116.       tab_seen = 1;
  117.     }
  118.       else if (BUF_FETCH_CHAR (buf, pos) == '\n' ||
  119.            (EQ (buf->selective_display, Qt) &&
  120.         BUF_FETCH_CHAR (buf, pos) == '\r'))
  121.     break;
  122.       else
  123.     {
  124.       /* #### This needs updating to handle the new redisplay. */
  125. #if 0
  126.       displayed_glyphs = glyphs_from_bufpos (sel_frame, buf,
  127.                          XWINDOW (selected_window),
  128.                          pos, dp, 0, col, 0, 0, 0);
  129.       col += (displayed_glyphs->columns
  130.           - (displayed_glyphs->begin_columns
  131.              + displayed_glyphs->end_columns));
  132. #else
  133.       col++;
  134. #endif
  135.     }
  136.     }
  137.  
  138.   if (tab_seen)
  139.     {
  140.       col = ((col + tab_width) / tab_width) * tab_width;
  141.       col += post_tab;
  142.     }
  143.  
  144.   if (cur_col)
  145.     {
  146.       last_known_column_buffer = buf;
  147.       last_known_column = col;
  148.       last_known_column_point = BUF_PT (buf);
  149.       last_known_column_modified = BUF_MODIFF (buf);
  150.     }
  151.  
  152.   return col;
  153. }
  154.  
  155. int
  156. current_column (struct buffer *buf)
  157. {
  158.   if (buf == last_known_column_buffer
  159.       && BUF_PT (buf) == last_known_column_point
  160.       && BUF_MODIFF (buf) == last_known_column_modified)
  161.     return last_known_column;
  162.  
  163.   return column_at_point (buf, BUF_PT (buf), 1);
  164. }
  165.  
  166. DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 1, 0,
  167.   "Return the horizontal position of point.  Beginning of line is column 0.\n\
  168. This is calculated by adding together the widths of all the displayed\n\
  169.  representations of the character between the start of the previous line\n\
  170.  and point. (e.g. control characters will have a width of 2 or 4, tabs\n\
  171.  will have a variable width.)\n\
  172. Ignores finite width of frame, which means that this function may return\n\
  173.  values greater than (frame-width).\n\
  174. Whether the line is visible (if `selective-display' is t) has no effect;\n\
  175.  however, ^M is treated as end of line when `selective-display' is t.\n\
  176. If BUFFER is nil, the current buffer is assumed.")
  177.   (buffer)
  178.      Lisp_Object buffer;
  179. {
  180.   return (make_number (current_column (decode_buffer (buffer, 0))));
  181. }
  182.  
  183.  
  184. DEFUN ("indent-to", Findent_to, Sindent_to, 1, 3, "NIndent to column: ",
  185.   "Indent from point with tabs and spaces until COLUMN is reached.\n\
  186. Optional second argument MIN says always do at least MIN spaces\n\
  187.  even if that goes past COLUMN; by default, MIN is zero.\n\
  188. If BUFFER is nil, the current buffer is assumed.")
  189.   (col, minimum, buffer)
  190.      Lisp_Object col, minimum, buffer;
  191. {
  192.   /* This function can GC */
  193.   int mincol;
  194.   int fromcol;
  195.   struct buffer *buf = decode_buffer (buffer, 0);
  196.   int tab_width = XINT (buf->tab_width);
  197.   Bufpos opoint = 0;
  198.  
  199.   CHECK_INT (col, 0);
  200.   if (NILP (minimum))
  201.     minimum = Qzero;
  202.   else
  203.     CHECK_INT (minimum, 1);
  204.  
  205.   XSETBUFFER (buffer, buf);
  206.   
  207.   fromcol = current_column (buf);
  208.   mincol = fromcol + XINT (minimum);
  209.   if (mincol < XINT (col)) mincol = XINT (col);
  210.  
  211.   if (fromcol == mincol)
  212.     return make_number (mincol);
  213.  
  214.   if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  215.   
  216.   if (!NILP (Fextent_at (make_number (BUF_PT (buf)), buffer, Qinvisible,
  217.              Qnil)))
  218.     {
  219.       Bufpos last_visible = last_visible_position (BUF_PT (buf), buf);
  220.  
  221.       opoint = BUF_PT (buf);
  222.       if (last_visible >= BUF_BEGV (buf))
  223.     BUF_SET_PT (buf, last_visible);
  224.       else 
  225.         error ("Visible portion of buffer not modifiable");
  226.     }
  227.  
  228.   if (indent_tabs_mode)
  229.     {
  230.       int n = mincol / tab_width - fromcol / tab_width;
  231.       if (n != 0)
  232.     {
  233.       Finsert_char (make_number ('\t'), make_number (n), buffer);
  234.  
  235.       fromcol = (mincol / tab_width) * tab_width;
  236.     }
  237.     }
  238.  
  239.   Finsert_char (make_number (' '), make_number (mincol - fromcol), buffer);
  240.  
  241.   last_known_column_buffer = buf;
  242.   last_known_column = mincol;
  243.   last_known_column_point = BUF_PT (buf);
  244.   last_known_column_modified = BUF_MODIFF (buf);
  245.  
  246.   if (opoint > 0)
  247.     BUF_SET_PT (buf, opoint);
  248.  
  249.   return (make_number (mincol));
  250. }
  251.  
  252. int
  253. spaces_at_point (struct buffer *b, Bufpos pos)
  254. {
  255.   Bufpos end = BUF_ZV (b);
  256.   int col = 0;
  257.   Emchar c;
  258.   int tab_width = XINT (b->tab_width);
  259.  
  260.   if (tab_width <= 0 || tab_width > 20)
  261.     tab_width = 8;
  262.  
  263.   while (pos < end &&
  264.      (c = BUF_FETCH_CHAR (b, pos),
  265.       (c == '\t'
  266.        ? (col += tab_width - col % tab_width)
  267.        : (c == ' ' ? ++col : 0))))
  268.     pos++;
  269.  
  270.   return col;
  271. }
  272.  
  273.  
  274. DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
  275.   0, 1, 0,
  276.   "Return the indentation of the current line.\n\
  277. This is the horizontal position of the character\n\
  278. following any initial whitespace.")
  279.   (buffer)
  280.   Lisp_Object buffer;
  281. {
  282.   struct buffer *buf = decode_buffer (buffer, 0);
  283.   Bufpos pos = find_next_newline (buf, BUF_PT (buf), -1);
  284.  
  285.   XSETBUFFER (buffer, buf);
  286.  
  287.   if (!NILP (Fextent_at (make_number (pos), buffer, Qinvisible, Qnil)))
  288.     return Qzero;
  289.  
  290.   return make_number (spaces_at_point (buf, pos));
  291. }
  292.  
  293.  
  294. DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 3, 0,
  295.   "Move point to column COLUMN in the current line.\n\
  296. The column of a character is calculated by adding together the widths\n\
  297. as displayed of the previous characters in the line.\n\
  298. This function ignores line-continuation;\n\
  299. there is no upper limit on the column number a character can have\n\
  300. and horizontal scrolling has no effect.\n\n\
  301. If specified column is within a character, point goes after that character.\n\
  302. If it's past end of line, point goes to end of line.\n\n\
  303. A non-nil second (optional) argument FORCE means, if the line\n\
  304. is too short to reach column COLUMN then add spaces/tabs to get there,\n\
  305. and if COLUMN is in the middle of a tab character, change it to spaces.\n\
  306. Returns the actual column that it moved to.")
  307.   (column, force, buffer)
  308.      Lisp_Object column, force, buffer;
  309. {
  310.   /* This function can GC */
  311.   Bufpos pos;
  312.   struct buffer *buf = decode_buffer (buffer, 0);
  313.   int col = current_column (buf);
  314.   int goal;
  315.   Bufpos end;
  316.   int tab_width = XINT (buf->tab_width);
  317.  
  318.   int prev_col = 0;
  319.   Emchar c = 0;
  320.  
  321.   XSETBUFFER (buffer, buf);
  322.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  323.   CHECK_NATNUM (column, 0);
  324.   goal = XINT (column);
  325.  
  326.  retry:
  327.   pos = BUF_PT (buf);
  328.   end = BUF_ZV (buf);
  329.  
  330.   /* If we're starting past the desired column,
  331.      back up to beginning of line and scan from there.  */
  332.   if (col > goal)
  333.     {
  334.       pos = find_next_newline (buf, pos, -1);
  335.       col = 0;
  336.     }
  337.  
  338.   while (col < goal && pos < end)
  339.     {
  340.       c = BUF_FETCH_CHAR (buf, pos);
  341.       if (c == '\n')
  342.     break;
  343.       if (c == '\r' && EQ (buf->selective_display, Qt))
  344.     break;
  345.       if (c == '\t')
  346.     {
  347.       prev_col = col;
  348.       col += tab_width;
  349.       col = col / tab_width * tab_width;
  350.     }
  351.       else
  352.     {
  353. /* #### oh for the days of the complete new redisplay */
  354. #if 0
  355.       displayed_glyphs = glyphs_from_bufpos (selected_frame (),
  356.                          buf,
  357.                          XWINDOW (Fselected_window (Qnil)),
  358.                          pos, dp, 0, col, 0, 0, 0);
  359.       col += (displayed_glyphs->columns
  360.           - (displayed_glyphs->begin_columns
  361.              + displayed_glyphs->end_columns));
  362. #else
  363.       col++;
  364. #endif
  365.     }
  366.  
  367.       pos++;
  368.     }
  369.  
  370.   BUF_SET_PT (buf, pos);
  371.  
  372.   /* If a tab char made us overshoot, change it to spaces
  373.      and scan through it again.  */
  374.   if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
  375.     {
  376.       buffer_delete_range (buf, BUF_PT (buf) - 1, BUF_PT (buf), 0);
  377.       Findent_to (make_number (col - 1), Qzero, buffer);
  378.       buffer_insert_emacs_char (buf, ' ');
  379.       goto retry;
  380.     }
  381.  
  382.   /* If line ends prematurely, add space to the end.  */
  383.   if (col < goal && !NILP (force))
  384.     {
  385.       col = goal;
  386.       Findent_to (make_number (col), Qzero, buffer);
  387.     }
  388.  
  389.   last_known_column_buffer = buf;
  390.   last_known_column = col;
  391.   last_known_column_point = BUF_PT (buf);
  392.   last_known_column_modified = BUF_MODIFF (buf);
  393.  
  394.   return (make_number (col));
  395. }
  396.  
  397. /*****************************************************************************
  398.  vmotion
  399.  
  400.  Given a starting position ORIG, move point VTARGET lines in WINDOW.
  401.  Returns the new value for point.  If the arg ret_vpos is not nil, it is
  402.  taken to be a pointer to an int and the number of lines actually moved is
  403.  returned in it.
  404.  ****************************************************************************/
  405. Bufpos
  406. vmotion (struct window *w, Bufpos orig, int vtarget, int *ret_vpos)
  407. {
  408.   struct buffer *b = XBUFFER (w->buffer);
  409.   int elt;
  410.  
  411.   elt = point_in_line_start_cache (w, orig, (vtarget < 0
  412.                          ? -vtarget
  413.                          : vtarget));
  414.  
  415.   /* #### This assertion must be true before the if statements are hit
  416.      but may possibly be wrong after the call to
  417.      point_in_line_start_cache if orig is outside of the visible
  418.      region of the buffer.  Handle this. */
  419.   assert (elt >= 0);
  420.  
  421.   /* Moving downward. */
  422.   if (vtarget > 0)
  423.     {
  424.       int cur_line = Dynarr_length (w->line_start_cache) - 1 - elt;
  425.       Bufpos ret_pt;
  426.  
  427.       if (cur_line > vtarget)
  428.     cur_line = vtarget;
  429.  
  430.       /* The traditional FSF behavior is to return the end of buffer
  431.          position if we couldn't move far enough because we hit it.  */
  432.       if (cur_line < vtarget)
  433.     ret_pt = BUF_ZV (b);
  434.       else
  435.     ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start;
  436.  
  437.       while (ret_pt > BUF_ZV (b) && cur_line > 0)
  438.     {
  439.       cur_line--;
  440.       ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start;
  441.     }
  442.  
  443.       if (ret_vpos) *ret_vpos = cur_line;
  444.       return (ret_pt);
  445.     }
  446.   else if (vtarget < 0)
  447.     {
  448.       if (elt < -vtarget)
  449.     {
  450.       if (ret_vpos) *ret_vpos = -elt;
  451.       /* #### This should be BUF_BEGV (b), right? */
  452.       return (Dynarr_atp (w->line_start_cache, 0)->start);
  453.     }
  454.       else
  455.     {
  456.       if (ret_vpos) *ret_vpos = vtarget;
  457.       return (Dynarr_atp (w->line_start_cache, elt + vtarget)->start);
  458.     }
  459.     }
  460.   else
  461.     {
  462.       /* No vertical motion requested so we just return the position
  463.          of the beginning of the current line. */
  464.       if (ret_vpos) *ret_vpos = 0;
  465.  
  466.       return (Dynarr_atp (w->line_start_cache, elt)->start);
  467.     }
  468.  
  469.   return 0;    /* shut up compiler */
  470. }
  471.  
  472. DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 2, 0,
  473.   "Move to start of frame line LINES lines down.\n\
  474. If LINES is negative, this is moving up.\n\
  475. Sets point to position found; this may be start of line\n\
  476.  or just the start of a continuation line.\n\
  477. Returns number of lines moved; may be closer to zero than LINES\n\
  478.  if beginning or end of buffer was reached.\n\
  479. Optional second argument is WINDOW to move in.")
  480.   (lines, window)
  481.      Lisp_Object lines, window;
  482. {
  483.   if (NILP (window))
  484.     window = Fselected_window (Qnil);
  485.   CHECK_WINDOW (window, 0);
  486.   {
  487.     Bufpos bufpos;
  488.     int vpos;
  489.     struct window *w  = XWINDOW (window);
  490.  
  491.     CHECK_INT (lines, 0);
  492.  
  493.     bufpos = vmotion (XWINDOW (window), BUF_PT (XBUFFER (w->buffer)),
  494.               XINT (lines), &vpos);
  495.  
  496.     /* Note that the buffer's point is set, not the window's point. */
  497.     BUF_SET_PT (XBUFFER (w->buffer), bufpos);
  498.  
  499.     return make_number (vpos);
  500.   }
  501. }
  502.  
  503.  
  504. void
  505. syms_of_indent (void)
  506. {
  507.   defsubr (&Scurrent_indentation);
  508.   defsubr (&Sindent_to);
  509.   defsubr (&Scurrent_column);
  510.   defsubr (&Smove_to_column);
  511.   defsubr (&Svertical_motion);
  512. }
  513.  
  514. void
  515. vars_of_indent (void)
  516. {
  517.   DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
  518.     "*Indentation can insert tabs if this is non-nil.\n\
  519. Setting this variable automatically makes it local to the current buffer.");
  520.   indent_tabs_mode = 1;
  521. }
  522.